package com.wwk.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.wwk.dao.OrderSettingDao;
import com.wwk.pojo.OrderSetting;
import com.wwk.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Classname OrderSettingServiceImpl
 * @Description TODO
 * @Date 2020/11/4 17:25
 * @Created by com.wwk
 * @Author com.wwk
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao settingDao;

    SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public void add(List<OrderSetting> list) {
        if(list!=null && list.size()>0){
            for (OrderSetting orderSetting : list) {
                //检查此数据（日期）是否存在
                long count = settingDao.findCountByOrderDate(f.format(orderSetting.getOrderDate()));
                if(count>0){
                    //已经存在，执行更新操作
                    Map map=new HashMap<>();
                    map.put("orderDate",f.format(orderSetting.getOrderDate()));
                    map.put("number",orderSetting.getNumber());
                    settingDao.editNumberByOrderDate(map);
                }
                else{
                    //不存在，执行添加操作
                    settingDao.addData2Base(orderSetting);
                }

            }
        }
    }

    @Override
    public List<Map> getOrderSettingByDate(String date) {//date 格式为2020-11
        //现在已有的数据是年月，要获得本月的OrderSetting信息，需要从数据库查出这个月的所有的OrderSetting，返回集合。
        String beginDate=date+"-1";
        String lastDate=null;
        //不能单纯的进行拼接，因为日期格式不对，数据库会报错
        final String[] split = date.split("-");
        switch (Integer.parseInt(split[1])){
            case 1 :
            case 3 :
            case 5 :
            case 7 :
            case 8 :
            case 10 :
            case 12 : lastDate=date+"-31";break;
            default:lastDate=date+"-30";break;
        }

        //创建一个Map接收日期范围数据，用于mybatis读取
        Map map=new HashMap<>();
        map.put("beginDate",beginDate);
        map.put("lastDate",lastDate);

        //从数据库中查出在日期范围内的所有OrderSetting
        List<OrderSetting> list=settingDao.findOrderSettingByDate(map);

        //得到集合数据后需要返回和客户端json格式相匹配的数据
        List<Map> orderSettingByDate=new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map orderSettingMap=new HashMap<>();
              /*this.leftobj = [
                        { date: 1, number: 120, reservations: 1 },
                        { date: 3, number: 120, reservations: 1 },
                        { date: 4, number: 120, reservations: 120 },
                        { date: 6, number: 120, reservations: 1 },
                        { date: 8, number: 120, reservations: 1 }
                    ];*/
            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());;//获得日期 （几号）
            orderSettingMap.put("number",orderSetting.getNumber());//可预约人数
            orderSettingMap.put("reservations",orderSetting.getReservations());//已预约人数
            orderSettingByDate.add(orderSettingMap);//加入到集合中
        }
        return orderSettingByDate;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        long count = settingDao.findCountByOrderDate(f.format(orderSetting.getOrderDate()));
        System.out.println("=================="+orderSetting.getOrderDate());
        System.out.println("========================="+count);
        if(count>0){
            //已经存在，执行更新操作
            Map map=new HashMap<>();
            map.put("orderDate",f.format(orderSetting.getOrderDate()));
            map.put("number",orderSetting.getNumber());
            settingDao.editNumberByOrderDate(map);
        }else{
            //当前日期没有进行预约设置，进行添加操作
            settingDao.addData2Base(orderSetting);
        }
    }
}
